import word from "@/assets/word.png";
import excel from "@/assets/excel.png";
import text from "@/assets/text.png";
import exe from "@/assets/exe.png";
import unknown from "@/assets/unknown.png";
import pdf from "@/assets/pdf.png";
import zip from "@/assets/zip.png";
import ppt from "@/assets/ppt.png";
export const createBlob = (file: any) => {
    const blob = new Blob([file], { type: file.type });
    const url = URL.createObjectURL(blob);
    return url;
};
export const getFileExtension = (fileName: string) => {
    const dotIndex = fileName.lastIndexOf(".");
    if (dotIndex > -1) {
        return fileName.slice(dotIndex + 1);
    }
    return ""; // 如果文件名中没有点，返回空字符串
};

export const determineFileType = (fileExtension: any, url: string) => {
    const fileTypes = {
        docx: { icon: word, type: "word" },
        pdf: { icon: pdf, type: "pdf" },
        pptx: { icon: ppt, type: "ppt" },
        txt: { icon: text, type: "textFile" },
        jpg: { icon: url, type: "image" },
        jpeg: { icon: url, type: "image" },
        png: { icon: url, type: "image" },
        gif: { icon: url, type: "image" },
        svg: { icon: url, type: "image" },
        mp3: { icon: url, type: "video" },
        mp4: { icon: url, type: "video" },
        avi: { icon: url, type: "video" },
        mov: { icon: url, type: "video" },
        zip: { icon: zip, type: "zip" },
        rar: { icon: zip, type: "zip" },
        tar: { icon: zip, type: "zip" },
        gz: { icon: zip, type: "zip" },
        exe: { icon: exe, type: "exe" },
        xlsx: { icon: excel, type: "excel" },
    } as any;

    return (
        fileTypes[fileExtension.toLowerCase()] || { icon: unknown, type: "unknown" }
    );
};

const drawPlayButton = (ctx: any, width: any, height: any) => {
    let buttonSize = Math.min(width, height) * 0.35;
    let x = (width - buttonSize) / 2;
    let y = (height - buttonSize) / 2;

    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x, y + buttonSize);
    ctx.lineTo(x + buttonSize, y + buttonSize / 2);
    ctx.closePath();

    ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
    ctx.shadowBlur = 8;
    ctx.shadowOffsetX = 3;
    ctx.shadowOffsetY = 3;

    const gradient = ctx.createLinearGradient(x, y, x + buttonSize, y + buttonSize);
    gradient.addColorStop(0, 'rgba(0, 120, 255, 1)');
    gradient.addColorStop(1, 'rgba(0, 180, 255, 1)');
    ctx.fillStyle = gradient;
    ctx.fill();

    ctx.shadowColor = 'transparent';
}

export const getVideoBobUrl = (url: string) => {
    return new Promise(function (resolve) {
        let dataURL = "";
        let video = document.createElement("video");
        video.setAttribute("crossOrigin", "anonymous"); //处理跨域
        video.setAttribute("src", url);
        video.setAttribute("width", (400 as any));
        video.setAttribute("height", (240 as any));

        video.addEventListener('loadeddata', function () {
            video.currentTime = 2;
            video.addEventListener('seeked', function onSeeked() {
                video.removeEventListener('seeked', onSeeked);
                try {
                    let canvas = document.createElement('canvas');
                    let width = video.width;
                    let height = video.height;
                    canvas.width = width * 2;
                    canvas.height = height * 2;
                    const ctx = canvas.getContext('2d') as any;
                    ctx.scale(2, 2);
                    ctx.drawImage(video, 0, 0, width, height);

                    drawPlayButton(ctx, width, height);
                    dataURL = canvas.toDataURL('image/jpeg', 1.0);
                    resolve(dataURL);
                } catch (error) {
                    console.error('Error generating video thumbnail:', error);
                    resolve('');
                }
            });
        });
        video.addEventListener('error', function () {
            console.error('Video loading error');
            resolve('');
        });
    });
};



export const debounce = (fn: any, delay: any) => {
    let timerId: any;
    return (...args: any) => {
        if (timerId) clearTimeout(timerId);
        timerId = setTimeout(() => fn(...args), delay);
    };
};